using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using thinger.ToolsLib;

namespace thinger.ControlDemo
{
    public partial class FrmSQLiteHelper : Form
    {
        public FrmSQLiteHelper()
        {
            InitializeComponent();

            this.dgv_Data.AutoGenerateColumns = false;


            this.txt_ConnStr.Text = $"Data Source={Application.StartupPath + "\\DataBase\\Equipment"};Polling=true;FailIfMissing=false";

            // this.txt_ConnStr.Text = $"Data Source=DataBase\\Equipment;Polling=true;FailIfMissing=false";

            // this.txt_ConnStr.Text = @"Data Source=C:\Users\fujiajin\Desktop\thinger.ToolsLibDemo\thinger.ToolsLibDemo\bin\Debug\DataBase\Equipment;Polling=true;FailIfMissing=false";


            SQLiteHelper.connString = this.txt_ConnStr.Text;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            string sql = "insert into Equipment(EquipId,EquipName) values({0},'{1}')";

            sql = string.Format(sql, Convert.ToInt32(this.txt_DevId.Text), this.txt_DevName.Text);

            SQLiteHelper.ExecuteNonQuery(sql);

            btn_Query_Click(null, null);
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            string sql = "Select EquipId,EquipName from Equipment";

            DataSet dataSet = SQLiteHelper.GetDataSet(sql);

            if (dataSet != null)
            {
                this.dgv_Data.DataSource = null;
                this.dgv_Data.DataSource = dataSet.Tables[0];
            }
        }

        private void dgv_Data_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.txt_DevId.Text = this.dgv_Data.SelectedRows[0].Cells[0].Value.ToString();

            this.txt_DevName.Text= this.dgv_Data.SelectedRows[0].Cells[1].Value.ToString();
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            string sql = "delete from Equipment where EquipId={0}";

            sql = string.Format(sql, Convert.ToInt32(this.txt_DevId.Text));

            SQLiteHelper.ExecuteNonQuery(sql);

            btn_Query_Click(null, null);
        }

        private void btn_Modify_Click(object sender, EventArgs e)
        {
            string sql = "update Equipment Set EquipName='{0}' where EquipId={1}";

            sql = string.Format(sql, this.txt_DevName.Text, Convert.ToInt32(this.txt_DevId.Text));

            SQLiteHelper.ExecuteNonQuery(sql);

            btn_Query_Click(null, null);
        }
    }
}
